package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/5/18
 */
@RestController
@RequestMapping("/admin/dashboard")
public class DtsAdminController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsGoodsProductService goodsProductService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsCategoryService categoryService;

    @RequestMapping("/chart")
    public Object chart(){
        Map<String,Object> data= new HashMap<>();
        List<CategorySellAmts> categoryShellDate=categoryService.findCategorySellData();

        String[] categoryNames = new String[categoryShellDate.size()];

        for (int i = 0; i <categoryShellDate.size() ; i++) {
            CategorySellAmts categorySellAmts=categoryShellDate.get(i);
            String name = categorySellAmts.getName();

            categoryNames[i] = name;
        }

        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(categoryNames);
        categorySellVo.setCategorySellData(categoryShellDate);

        List<DayStatis> orderStatis = orderService.getOrderAmts();

        List<String> dayDataList =fetchDateData(orderStatis);

        String[] dayData = dayDataList.toArray(new String[]{});

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(dayData);

        List<BigDecimal> orderAmtList = orderStatis.stream().map(orderStati->{
            return orderStati.getAmts();
        }).collect(Collectors.toList());

        orderAmtsVo.setOrderAmtData(orderAmtList.toArray(new BigDecimal[]{}));
        List<Integer> orderCntsList = orderStatis.stream().map(orderStati->{
            return orderStati.getCnts();
        }).collect(Collectors.toList());
        Integer[] ordercnts = orderCntsList.toArray(new Integer[]{});

        orderAmtsVo.setOrderCntData(ordercnts);
        List<DayStatis> userDayStatis = userService.findUserStatis();
        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        userOrderCntVo.setDayData(dayData);
        userOrderCntVo.setOrderCnt(ordercnts);

        List<Integer> userCnts = userDayStatis.stream().map(dayStati -> {
            return dayStati.getCnts();
        }).collect(Collectors.toList());
        userOrderCntVo.setUserCnt(userCnts.toArray(new Integer[]{}));

        data.put("categorySell",categorySellVo);
        data.put("orderAmts", orderAmtsVo);
        data.put("userOrderCnt", userOrderCntVo);

        return ResponseUtil.ok(data);
    }

    private List<String> fetchDateData(List<DayStatis> orderStatis) {
        return  orderStatis.stream().map(orderStati->{
            return orderStati.getDayStr();
        }).collect(Collectors.toList());
    }

    @RequestMapping("")
    public Object init(){
        Map<String,Object> data = new HashMap<>();

        Integer goodsTotal =goodsService.getTotal();
        data.put("goodsTotal", goodsTotal);
        Integer userTotal = userService.getTotal();
        data.put("userTotal", userTotal);
        Integer productTotal =goodsProductService.getTotal();
        data.put("productTotal", productTotal);
        Integer orderTotal = orderService.getTotal();
        data.put("orderTotal", orderTotal);
        return ResponseUtil.ok(data);

    }
}
